﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Snake_Communication
{
    public partial class MySnake : Form
    {
        Snake snake;
        Graphics paper;
        Random rand = new Random();
        Pen penBordure = new Pen(Color.White, 2);
        System.Media.SoundPlayer sp;
        Food food;
        bool left = false;
        bool right = false;
        bool up = false;
        bool down = false;
        public MySnake()
        {
            InitializeComponent();
            snake = new Snake();
            food = new Food(rand);
            sp = new System.Media.SoundPlayer(@"C:\Users\Florian\Documents\Visual Studio 2013\Projects\Project1\WindowsFormsApplication1\Original Tetris theme (Tetris Soundtrack).wav");
            //sp.PlayLooping();
        }

        // supprime les scintillements
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;

                cp.ExStyle |= 0x02000000;
                //WS_EX_COMPOSITED. Prevents flickering.
                cp.ExStyle |= 0x00080000; //WS_EX_LAYERED. Transparency key

                return cp;
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

            paper = e.Graphics;
            //BORDURE
            penBordure.Color = Color.White;

            //NORTH
            paper.DrawLine(penBordure, new Point(42, 42), new Point(229, 42));
            
            paper.DrawLine(penBordure, new Point(269, 42), new Point(456, 42));
            paper.DrawLine(penBordure, new Point(496, 42), new Point(683, 42));

            //SOUTH
            paper.DrawLine(penBordure, new Point(42, 683), new Point(229, 683));
            paper.DrawLine(penBordure, new Point(269, 683), new Point(456, 683));
            paper.DrawLine(penBordure, new Point(496, 683), new Point(683, 683));

            //WEST
            paper.DrawLine(penBordure, new Point(42, 42), new Point(42, 229));
            paper.DrawLine(penBordure, new Point(42, 269), new Point(42, 456));
            paper.DrawLine(penBordure, new Point(42, 496), new Point(42, 683));

            //EAST
            paper.DrawLine(penBordure, new Point(683, 42), new Point(683, 229));
            paper.DrawLine(penBordure, new Point(683, 269), new Point(683, 456));
            paper.DrawLine(penBordure, new Point(683, 496), new Point(683, 683));
            

            //PORTES

            // WEST NORTH
            if (snake.hasBonus("WN"))
            {
                this.WNStatus.Text = "ON";
                this.WNStatus.ForeColor = System.Drawing.Color.Green;
                penBordure.Color = Color.Green;
            }
            else 
            {
                penBordure.Color = Color.Red;
            }
            paper.DrawLine(penBordure, new Point(42, 229), new Point(42, 269));
            // WEST SOUTH
            if (snake.hasBonus("WS"))
            {
                this.WSStatus.Text = "ON";
                this.WSStatus.ForeColor = System.Drawing.Color.Green;
                penBordure.Color = Color.Green;
            }
            else
            {
                penBordure.Color = Color.Red;
            }
            paper.DrawLine(penBordure, new Point(42, 456), new Point(42, 496));
            // EAST NORTH
            if (snake.hasBonus("EN"))
            {
                this.ENStatus.Text = "ON";
                this.ENStatus.ForeColor = System.Drawing.Color.Green;
                penBordure.Color = Color.Green;
            }
            else
            {
                penBordure.Color = Color.Red;
            }
            paper.DrawLine(penBordure, new Point(683, 229), new Point(683, 269));
            // EAST SOUTH
            if (snake.hasBonus("ES"))
            {
                this.ESStatus.Text = "ON";
                this.ESStatus.ForeColor = System.Drawing.Color.Green;
                penBordure.Color = Color.Green;
            }
            else
            {
                penBordure.Color = Color.Red;
            }
            paper.DrawLine(penBordure, new Point(683, 456), new Point(683, 496));
            // NORTH WEST
            if (snake.hasBonus("NW"))
            {
                this.NWStatus.Text = "ON";
                this.NWStatus.ForeColor = System.Drawing.Color.Green;
                penBordure.Color = Color.Green;
            }
            else
            {
                penBordure.Color = Color.Red;
            }
            paper.DrawLine(penBordure, new Point(229, 42), new Point(269, 42));
            // NORTH EAST
            if (snake.hasBonus("NE"))
            {
                this.NEStatus.Text = "ON";
                this.NEStatus.ForeColor = System.Drawing.Color.Green;
                penBordure.Color = Color.Green;
            }
            else
            {
                penBordure.Color = Color.Red;
            }
            paper.DrawLine(penBordure, new Point(456, 42), new Point(496, 42));
            // SOUTH WEST
            if (snake.hasBonus("SW"))
            {
                this.SWStatus.Text = "ON";
                this.SWStatus.ForeColor = System.Drawing.Color.Green;
                penBordure.Color = Color.Green;
            }
            else
            {
                penBordure.Color = Color.Red;
            }
            paper.DrawLine(penBordure, new Point(229, 683), new Point(269, 683));
            // SOUTH EAST
            if (snake.hasBonus("SE"))
            {
                this.SEStatus.Text = "ON";
                this.SEStatus.ForeColor = System.Drawing.Color.Green;
                penBordure.Color = Color.Green;
            }
            else
            {
                penBordure.Color = Color.Red;
            }
            paper.DrawLine(penBordure, new Point(456, 683), new Point(496, 683));
            food.drawFood(paper);
            //SNAKE
            snake.drawSnake(paper);

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.KeyData == Keys.Space)
            {
                if (spaceBarLabel.Text == "")
                {
                    timer1.Enabled = !timer1.Enabled;
                }
                else
                {
                    timer1.Enabled = true;
                    spaceBarLabel.Text = "";
                    down = false;
                    up = false;
                    left = false;
                    right = true;
                }
            }
            if (e.KeyData == Keys.Down && up == false)
            {
                down = true;
                up = false;
                right = false;
                left = false;
            }
            if (e.KeyData == Keys.Up && down == false)
            {
                down = false;
                up = true;
                right = false;
                left = false;
            }
            if (e.KeyData == Keys.Left && right == false)
            {
                down = false;
                up = false;
                right = false;
                left = true;
            }
            if (e.KeyData == Keys.Right && left == false)
            {
                down = false;
                up = false;
                right = true;
                left = false;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            if (down) { snake.moveDown(); }
            if (up) { snake.moveUp(); }
            if (right) { snake.moveRight(); }
            if (left) { snake.moveLeft(); }
            this.Invalidate();
            collision();
            if (snake.isAllBonusLoaded()==true)
            {
                winner();
            }
            for (int i = 0; i < snake.SnakeRec.Length; i++)
            {
                if (snake.SnakeRec[i].IntersectsWith(food.foodRec))
                {
                        Console.WriteLine("BONUS LOADED");
                        snake.addRandomBonus();
                        snake.growSnake();
                        food.foodlocation(rand);
                }
            }
            

        }

        public void collision()
        {
            for (int i = 1; i < snake.SnakeRec.Length; i++)
            {
                if (snake.SnakeRec[0].IntersectsWith(snake.SnakeRec[i]))
                {
                    restart();
                }
            }
            //NORTH
            if (((snake.SnakeRec[0].X > 42 && snake.SnakeRec[0].X < 229)||(snake.SnakeRec[0].X > 269 && snake.SnakeRec[0].X < 456)||(snake.SnakeRec[0].X > 496 && snake.SnakeRec[0].X < 683))&&(snake.SnakeRec[0].Y<=42))
            {
                Console.WriteLine("Collision NORTH");
                restart();

            }

            //SOUTH
            if (((snake.SnakeRec[0].X > 42 && snake.SnakeRec[0].X < 229) || (snake.SnakeRec[0].X > 269 && snake.SnakeRec[0].X < 456) || (snake.SnakeRec[0].X > 496 && snake.SnakeRec[0].X < 683)) && (snake.SnakeRec[0].Y >= 683))
            {
                Console.WriteLine("Collision SOUTH");
                restart();

            }

            //WEST
            if (((snake.SnakeRec[0].Y > 42 && snake.SnakeRec[0].Y < 229) || (snake.SnakeRec[0].Y > 269 && snake.SnakeRec[0].Y < 456) || (snake.SnakeRec[0].Y > 496 && snake.SnakeRec[0].Y < 683)) && (snake.SnakeRec[0].X <= 42))
            {
                Console.WriteLine("Collision WEST");
                restart();
            }

            //EAST
            if (((snake.SnakeRec[0].Y > 42 && snake.SnakeRec[0].Y < 229) || (snake.SnakeRec[0].Y > 269 && snake.SnakeRec[0].Y < 456) || (snake.SnakeRec[0].Y > 496 && snake.SnakeRec[0].Y < 683)) && (snake.SnakeRec[0].X >= 683))
            {
                Console.WriteLine("Collision EAST");
                restart();
            }


            //DOORS

            //NW
            if((snake.SnakeRec[0].X > 229 && snake.SnakeRec[0].X < 269) && snake.SnakeRec[0].Y < 42)
            {
                if(snake.hasBonus("NW"))
                {
                    timer1.Enabled = false;
                    MessageBox.Show("AFFICHER NW");
                }
                else 
                {
                    Console.WriteLine("Collision NORTH");
                    restart();
                }
            }

            //NE
            if ((snake.SnakeRec[0].X > 456 && snake.SnakeRec[0].X < 496) && snake.SnakeRec[0].Y < 42)
            {
                if (snake.hasBonus("NE"))
                {
                    timer1.Enabled = false;
                    MessageBox.Show("AFFICHER NE");
                }
                else
                {
                    Console.WriteLine("Collision NORTH");
                    restart();
                }
            }

            //SW
            if ((snake.SnakeRec[0].X > 229 && snake.SnakeRec[0].X < 269) && snake.SnakeRec[0].Y >683)
            {
                if (snake.hasBonus("SW"))
                {
                    timer1.Enabled = false;
                    MessageBox.Show("AFFICHER SW");
                }
                else
                {
                    Console.WriteLine("Collision SOUTH");
                    restart();
                }
            }

            //SE
            if ((snake.SnakeRec[0].X > 456 && snake.SnakeRec[0].X < 496) && snake.SnakeRec[0].Y > 683)
            {
                if (snake.hasBonus("SE"))
                {
                    timer1.Enabled = false;
                    MessageBox.Show("AFFICHER SE");
                }
                else
                {
                    Console.WriteLine("Collision SOUTH");
                    restart();
                }
            }


            //WN
            if ((snake.SnakeRec[0].Y > 229 && snake.SnakeRec[0].Y < 269) && snake.SnakeRec[0].X < 42)
            {
                if (snake.hasBonus("WN"))
                {
                    timer1.Enabled = false;
                    MessageBox.Show("AFFICHER WN");
                }
                else
                {
                    Console.WriteLine("Collision WEST");
                    restart();
                }
            }

            //WS
            if ((snake.SnakeRec[0].Y > 456 && snake.SnakeRec[0].Y < 496) && snake.SnakeRec[0].X < 42)
            {
                if (snake.hasBonus("WS"))
                {
                    timer1.Enabled = false;
                    MessageBox.Show("AFFICHER WS");
                }
                else
                {
                    Console.WriteLine("Collision WEST");
                    restart();
                }
            }

            //EN
            if ((snake.SnakeRec[0].Y > 229 && snake.SnakeRec[0].Y < 269) && snake.SnakeRec[0].X > 683)
            {
                if (snake.hasBonus("EN"))
                {
                    timer1.Enabled = false;
                    MessageBox.Show("AFFICHER EN");
                }
                else
                {
                    Console.WriteLine("Collision EAST");
                    restart();
                }
            }

            //ES
            if ((snake.SnakeRec[0].Y > 456 && snake.SnakeRec[0].Y < 496) && snake.SnakeRec[0].X > 683)
            {
                if (snake.hasBonus("ES"))
                {
                    timer1.Enabled = false;
                    MessageBox.Show("AFFICHER ES");
                }
                else
                {
                    Console.WriteLine("Collision EAST");
                    restart();
                }
            }
        }
        public void restart()
        {
            timer1.Enabled = false;
            sp.Stop();
            MessageBox.Show("GAME OVER");

            spaceBarLabel.Text = "Press SPACECAR to begin";

            snake = new Snake();


        }

        public void winner()
        {
            Console.WriteLine("YOU WON");
            timer1.Enabled = false;
            sp.Stop();
            MessageBox.Show("YOU WON");
            snake = new Snake();
        }

        private void soundCheck_CheckedChanged(object sender, EventArgs e)
        {
            if (this.soundCheck.Checked)
            {
                this.soundCheck.Text = "Sound ON";
                this.sp.PlayLooping();
            }
            else
            {
                this.soundCheck.Text = "Sound OFF";
                this.sp.Stop();
            }
            this.Focus();
        }

    }
}
